from sys import intern
import threading
import time

from Ctgust_BMS.monitor.ina219 import INA219

class INA219Thread(threading.Thread):
    def __init__(self,args):
        super().__init__(args=args)
        self.slave1 = args[0]
    def run(self):
        ina1 = INA219(addr=0x40)
        ina2 = INA219(addr=0x41)
        ina3 = INA219(addr=0x42)
        ina4 = INA219(addr=0x43)
    
        print("ina219 test")

        while True:
            bus_voltage1 = ina1.getBusVoltage_V()             # voltage on V- (load side)
            shunt_voltage1 = ina1.getShuntVoltage_mV() / 1000 # voltage between V+ and V- across the shunt
            current1 = ina1.getCurrent_mA()                   # current in mA
            power1 = ina1.getPower_W()                        # power in watts

            bus_voltage2 = ina2.getBusVoltage_V()             # voltage on V- (load side)
            shunt_voltage2 = ina2.getShuntVoltage_mV() / 1000 # voltage between V+ and V- across the shunt
            current2 = ina2.getCurrent_mA()                   # current in mA
            power2 = ina2.getPower_W()                        # power in watts

            bus_voltage3 = ina3.getBusVoltage_V()             # voltage on V- (load side)
            shunt_voltage3 = ina3.getShuntVoltage_mV() / 1000 # voltage between V+ and V- across the shunt
            current3 = ina3.getCurrent_mA()                   # current in mA
            power3 = ina3.getPower_W()                        # power in watts

            bus_voltage4 = ina4.getBusVoltage_V()             # voltage on V- (load side)
            shunt_voltage4 = ina4.getShuntVoltage_mV() / 1000 # voltage between V+ and V- across the shunt
            current4 = ina4.getCurrent_mA()                   # current in mA
            power4 = ina4.getPower_W()                        # power in watts
            
            # INA219 measure bus voltage on the load side. So PSU voltage = bus_voltage + shunt_voltage
            #print("PSU V:{:6.3f}V    Shunt V:{:9.6f}V    Load V:{:6.3f}V    Power:{:9.6f}W    C:{:9.6f}A".format((bus_voltage1 + shunt_voltage1),(shunt_voltage1),(bus_voltage1),(power1),(current1/1000)))
            #print("PSU V:{:6.3f}V    Shunt V:{:9.6f}V    Load V:{:6.3f}V    Power:{:9.6f}W    C:{:9.6f}A".format((bus_voltage2 + shunt_voltage2),(shunt_voltage2),(bus_voltage2),(power2),(current2/1000)))
            #print("PSU V:{:6.3f}V    Shunt V:{:9.6f}V    Load V:{:6.3f}V    Power:{:9.6f}W    C:{:9.6f}A".format((bus_voltage3 + shunt_voltage3),(shunt_voltage3),(bus_voltage3),(power3),(current3/1000)))
            #print("PSU V:{:6.3f}V    Shunt V:{:9.6f}V    Load V:{:6.3f}V    Power:{:9.6f}W    C:{:9.6f}A".format((bus_voltage4 + shunt_voltage4),(shunt_voltage4),(bus_voltage4),(power4),(current4/1000)))
            
            #print("")
            self.slave1.set_values('a', 1, [
                int(1000*bus_voltage1),abs(int(1000*shunt_voltage1)),abs(int(1000*current1)),int(1000*power1),     
            int(1000*bus_voltage2),abs(int(1000*shunt_voltage2)),abs(int(1000*current1)),int(1000*power2),
            int(1000*bus_voltage3),abs(int(1000*shunt_voltage3)),abs(int(1000*current1)),int(1000*power3),
            int(1000*bus_voltage4),abs(int(1000*shunt_voltage4)),abs(int(1000*current1)),int(1000*power4),
            ])  
            time.sleep(1)
